layui.config({
    base: '/layui1/lay/modules/'      //自定义layui组件的目录
}).extend({ //设定组件别名
    treeTable: 'treeTable'
});
layui.use(['layer', 'util', 'treeTable', 'jquery', 'form', 'tree'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var tree = layui.tree;
    var treeTable = layui.treeTable;
    var index;

    // 渲染表格
    var insTb = treeTable.render({
        elem: '#deptTreeTb',
        url: '/dept/findAllDeptTreeTable',
        toolbar: '#toolbarDemo',
        height: '400',
        cellMinWidth: 80,
        tree: {
            iconIndex: 2,
            isPidData: true,
            idName: 'deptId',
            pidName: 'parentId'
        },
        cols: [
            [
                {type: 'numbers'},
                {field: 'deptId', title: '部门编号'},
                {field: 'deptName', title: '部门名称', minWidth: 150},
                {field: 'orderNum', title: '排序'},
                {
                    field: 'status', title: '状态', align: 'center', templet: function (d) {
                        if (d.status == 0) {
                            return "<button class='layui-btn layui-btn-xs layui-btn-radius layui-btn-normal'>正常</button>"
                        } else {
                            return "<button class='layui-btn layui-btn-xs layui-btn-radius layui-btn-danger'>异常</button>"
                        }
                    }
                },
                {field: 'createBy', title: '创建人', align: 'center'}
                , {field: 'updateBy', title: '修改人', align: 'center'}
                , {field: 'createTime', title: '创建时间 ', align: 'center'}
                , {field: 'updateTime', title: '修改时间', align: 'center'}
                , {field: 'remark', title: '备注', width: '10%', align: 'center'}
                , {fixed: 'right', title: "操作", align: 'center', toolbar: '#tbBar', minWidth: 150}
            ]
        ],
        style: 'margin-top:0;'
    });
    reClick();

    function reClick() {

        // 全部展开
        $('#btnExpandAll').click(function () {
            insTb.expandAll();
        });

        // 全部折叠
        $('#btnFoldAll').click(function () {
            insTb.foldAll();
        });
    }

    // 工具列点击事件
    treeTable.on('tool(deptTreeTb)', function (obj) {
        var event = obj.event;
        if (event === 'del') {

            var deptId = obj.data.deptId;

            if (obj.data.hasOwnProperty('children') && obj.data.children.length > 0) {
                layer.alert("含有子菜单，禁止删除!");
            } else {
                //对于删除或者是其他敏感操作，一定要让用户确认。
                layer.confirm('确定要删除吗？', function () {

                    $.ajax({
                        url: '/dept/deleteDept',
                        dataType: 'json',
                        type: 'POST',
                        data: {deptId: deptId},//将js对象转成json串传入到后台
                        success: function (result) {
                            layer.closeAll();//关闭对话框
                            layer.msg(result.msg);
                            insTb.reload();
                            reClick();
                        }
                    })

                })
            }

        } else if (event === 'edit') {

            layer.open({
                title: ["编辑菜单", "color:#009688"],
                type: 1,
                closeBtn: 2,
                anim: 4,
                content: $('#update_dept_form'),
            });
            //获取父节点的名称
            $.ajax({
                url: '/dept/findParentNameId',
                dataType: 'json',
                type: 'POST',
                data: {deptId: obj.data.parentId},
                success: function (result) {
                    //将修改前的数据赋值给修改表单
                    form.val('update_dept_form_filter', {
                        "parentName": result.data.deptName
                        , "parentId": obj.data.parentId
                        , "deptId": obj.data.deptId
                        , "deptType": obj.data.deptType
                        , "deptName": obj.data.deptName
                        , "status": obj.data.status == 0 ? true : false
                        , "orderNum": obj.data.orderNum
                        , "remark": obj.data.remark
                    });
                }
            })

        }
    });

    //监听修改部门表单的提交
    form.on("submit(formUpdateDept)", function (data) {
        data.field.status = data.field.status ? 0 : 1;
        $.ajax({
            url: "/dept/updateDept",
            type: "post",
            data: data.field,
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    layer.closeAll();//关闭对话框
                    insTb.reload();
                    layer.msg("修改成功");
                    reClick();
                }
            }
        });
        return false;
    });

    // 头部工具栏点击事件
    treeTable.on('toolbar(deptTreeTb)', function (obj) {

        if (obj.event === 'addDept') {
            $.ajax({
                url: '/dept/findAllDept',
                dataType: 'json',
                contentType: "application/json;charset=UTF-8",
                type: 'GET',
                success: function (result) {
                    //基本演示
                    tree.render({
                        elem: '#deptTreeChoose'
                        , data: result
                        , showLine: false
                        , onlyIconControl: true
                        , accordion: true
                        , click: function (obj) {
                            $(".parent_id").val(obj.data.id);
                            $(".parent_name").val(obj.data.title);

                            $("#deptType").attr("disabled","disabled");
                            if (obj.data.children == null) {
                                form.val("save_dept_form_filter", {
                                    "deptType": "L"
                                });
                            } else {
                                form.val("save_dept_form_filter", {
                                    "deptType": "A"
                                });
                            }

                            layer.close(index);
                        }
                    });

                }
            });

            layer.open({
                title: ["添加部门", "color:#009688"],
                type: 1,
                closeBtn: 2,
                anim: 4,
                content: $('#save_dept_form'),
            });
        }
    });

    //监听保存部门表单的提交
    form.on("submit(formSaveDept)", function (data) {
        data.field.status = data.field.status ? 0 : 1;
        console.log(data.field)
        $.ajax({
            url: "/dept/saveDept",
            type: "post",
            data: data.field,
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    layer.closeAll();//关闭对话框
                    $("#save_dept_form")[0].reset();
                    layer.msg("添加成功");
                    insTb.reload();
                    reClick();
                    $("#deptType").prop("disabled",false);
                }
            }
        });
        return false;
    });
    //点击上级菜单弹出菜单树
    $(".parent_name").click(function () {
        index = layer.open({
            title: '选择上级菜单',
            type: 1,
            closeBtn: 2,
            anim: 4,
            content: $('#choose_parent_dept'),
            area: ['400px', '350px']
        });

    })
});